﻿using System;
using System.Windows.Forms;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理.Forms
{
    public delegate void SelectedHandle(object selected);
    public partial class FmSelectMenu : Form
    {
        public FmSelectMenu()
        {
            InitializeComponent();
        }
        public FmSelectMenu(string type, object selectedObj, System.Data.SqlClient.SqlConnection sqlConnection)
        {
            InitializeComponent();
            _type = type;
            _selectedObj = selectedObj;
            _sqlConnection = sqlConnection;
        }
        private string _type;
        private object _selectedObj;
        private System.Data.SqlClient.SqlConnection _sqlConnection;
        public event SelectedHandle Selected;


        private void FmSelectMenu_Load(object sender, EventArgs e)
        {
            this.dgvSelect.BackgroundColor = Common.MyColor.NewColor_Lavender;
            this.btnCancel.BackColor = Common.MyColor.NewColor_Lavender;
            this.btnOk.BackColor = Common.MyColor.NewColor_Lavender;
            this.BackColor = Common.MyColor.NewColor_LightLavender;
            this.dgvSelect.DoubleBuffered(true);
            switch (_type)
            {
                case "customer":
                    Customer customer = (Customer)_selectedObj;
                    this.dgvSelect.DataSource = SqlClassLibrary.SqlGetSet.GetCustomerDT(customer, _sqlConnection);
                    break;
                case "hetong":

                    break;
                case "dictionary":
                    Dictionary dictionary = (Dictionary)_selectedObj;
                    this.dgvSelect.DataSource = SqlClassLibrary.SqlGetSet.GetColumnDT(dictionary.Colname, _sqlConnection);
                    break;
            }
            if (dgvSelect.ColumnCount == 0)
            {
                this.dgvSelect.Columns.Add("提示", "提示");
                this.dgvSelect.Rows.Add("没有查到任何信息！");
                return;
            }
            int fmSelWidth = 10 * dgvSelect.ColumnCount;
            for (int i = 0; i < dgvSelect.ColumnCount; i++)
            {
                if (dgvSelect.Columns[i].Visible)
                    fmSelWidth = fmSelWidth + dgvSelect.Columns[i].Width;
            }

            this.Size = new System.Drawing.Size(fmSelWidth, this.Size.Height);
        }
        private void DgvSelect_DataSourceChanged(object sender, EventArgs e)
        {
            DrawingImage.ReNameDataGridView(this.dgvSelect, _sqlConnection);
            DrawingImage.ColorLines(this.dgvSelect, 1, new System.Drawing.Color[] { Common.MyColor.NewColor_LightLightGray, System.Drawing.Color.White });
        }

        #region User operation section
        private void BtnOk_Click(object sender, EventArgs e)
        {
            ActionSelected();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DgvSelect_DoubleClick(object sender, EventArgs e)
        {
            ActionSelected();
        }

        private void DgvSelect_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ActionSelected();
            }
        }

        #endregion



        private void ActionSelected()
        {
            if (this.dgvSelect.Rows.Count > 0)
            {
                object sel_obj = new object();
                switch (_type)
                {
                    case "customer":
                        sel_obj = DrawingImage.DataGridViewSelectedRowToObject(this.dgvSelect, new Customer());
                        break;
                    case "hetong":
                        sel_obj = DrawingImage.DataGridViewSelectedRowToObject(this.dgvSelect, new Hetong());
                        break;
                    case "dictionary":
                        sel_obj = DrawingImage.DataGridViewSelectedRowToObject(this.dgvSelect, new Dictionary());
                        break;
                }
                Selected(sel_obj);
            }

            this.Close();
        }


    }
}
